package com.example.exception;

import com.example.common.Result;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

@ControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {

    @ExceptionHandler(MaxUploadSizeExceededException.class)
    @ResponseBody
    public Result handleMaxSizeException(MaxUploadSizeExceededException exc) {
        // 设置响应状态为413 Payload Too Large
        HttpStatus status = HttpStatus.PAYLOAD_TOO_LARGE;
        // 返回自定义的错误信息
        return Result.error("文件大小不能超过10M！");
    }

    // 可以添加其他异常处理方法
}